//
//  OrderInfoView.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/8.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "OrderInfoView.h"
#import "MapInfoScrollerView.h"
#import "CallCarModel.h"

@interface OrderInfoView ()<OrderHandleViewDelegate , MapInfoScrollerViewDelegate>

@end

@implementation OrderInfoView {
    OrderHeaderView *_headerView;
    MapInfoScrollerView *_scroller;
    OrderHandleView *_handleView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    frame = kSCREEN_BOUNDS;
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor clearColor];
        UIView * display = [[UIView alloc] init];
        self.addSubview(display);
        __weak typeof(self) weakSelf = self;
        [display mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(6));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(6));
            make.top.equalTo(weakSelf.mas_top).mas_offset(kScaleHeight(60));
            make.height.mas_equalTo(kScaleHeight(320));
        }];
        display.layer.cornerRadius = kScaleWidth(8);
        _displayView = display;
        display.backgroundColor = [UIColor colorWithString:@"#F09636"];
        [self setup];
    }
    return self;
}

- (void)setup {
    _displayView.layer.masksToBounds = YES;
    _headerView = [[OrderHeaderView alloc] init];
    _handleView = [[OrderHandleView alloc] init];
    _scroller = [[MapInfoScrollerView alloc] init];
    _scroller.distanceDelegate = self;
    UIImageView * notiImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notice"]];
    UIButton * cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancel setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    _displayView.addSubview(_headerView).addSubview(_handleView).addSubview(_scroller).addSubview(notiImage).addSubview(cancel);
    _handleView.delegate = self;
    __weak typeof(self.displayView) weakSelf = self.displayView;
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.equalTo(weakSelf);
        make.height.mas_equalTo(kScaleHeight(80));
    }];
    [_scroller mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(_headerView.mas_bottom);
        make.bottom.equalTo(_handleView.mas_top);
    }];
    [_handleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kScaleHeight(55));
        make.bottom.left.right.equalTo(weakSelf);
    }];
    [notiImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(weakSelf);
        make.size.mas_equalTo((CGSize){kScaleWidth(40),kScaleWidth(40)});
    }];
    [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top).mas_offset(kScaleHeight(9));
        make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleHeight(9));
        make.size.mas_equalTo((CGSize){kScaleWidth(20),kScaleWidth(20)});
    }];
    _handleView.backgroundColor = [UIColor whiteColor];
    _scroller.backgroundColor = [UIColor grayColor];
    _headerView.backgroundColor = [UIColor clearColor];
    [cancel addTarget:self action:@selector(cancelEvent) forControlEvents:UIControlEventTouchUpInside];
}

- (void)getDistance:(NSString *)distance time:(NSString *)time {
    _headerView.distance = distance;
    _headerView.time = time;
    _headerView.avatarUrl = _model.headPic;
}

- (void)cancelEvent {
    [self removeFromToWindows];
}

- (void)removeFromToWindows {
    if([_delegate respondsToSelector:@selector(remove)] && _delegate) {
        [_delegate remove];
    }
    [UIView animateWithDuration:0.25f animations:^{
        self.alpha = 0;
        _displayView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)clickWords:(OrderHandleView *)handle {
    _scroller.isMap = NO;
}

- (void)clickMap:(OrderHandleView *)handle {
    _scroller.isMap = YES;
}

- (void)setModel:(CallCarModel *)model {
    _model = model;
    _scroller.model = model;
}

@end


/**
 *  header
 */
@implementation OrderHeaderView {
    UILabel *_distanceLabel;
    UILabel *_dateLabel;
    UIButton *_cancelButton;
    UIImageView *_passengerAvatarImageView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        _distanceLabel = [[UILabel alloc] init];
        _dateLabel = [[UILabel alloc] init];
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _distanceLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _distanceLabel.textColor = [UIColor whiteColor];
        _dateLabel.textColor = [UIColor whiteColor];
        _distanceLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:kFontSize(16)];
        _passengerAvatarImageView = [[UIImageView alloc] init];
        [_passengerAvatarImageView setImage:[UIImage imageNamed:@"default_avatar"]];
        _passengerAvatarImageView.layer.cornerRadius = 28;
        _passengerAvatarImageView.layer.masksToBounds = YES;
        self.addSubview(_distanceLabel).addSubview(_dateLabel).addSubview(_cancelButton).addSubview(_passengerAvatarImageView);
        __weak typeof(self) weakSelf = self;
        [_passengerAvatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(56);
            make.left.equalTo(weakSelf.mas_left).mas_offset(60);
            make.centerY.equalTo(weakSelf.mas_centerY);
        }];
        [_distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_passengerAvatarImageView.mas_right).mas_offset(10);
            make.right.equalTo(weakSelf.mas_right).mas_offset(-25);
            make.top.equalTo(_passengerAvatarImageView.mas_top);
        }];
        [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_distanceLabel.mas_left);
            make.bottom.equalTo(_passengerAvatarImageView.mas_bottom);
        }];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
           make.right.equalTo(weakSelf.mas_right).mas_offset(kScaleWidth(3));
           make.top.equalTo(weakSelf.mas_top).mas_offset(kScaleHeight(3));
        }];
    }
    return self;
}

- (void)setTime:(NSString *)time {
    _time = time;
    _dateLabel.text = time;
}

- (void)setDistance:(NSString *)distance {
    _distance = distance;
    _distanceLabel.text = distance;
}

- (void)setAvatarUrl:(NSString *)avatarUrl {
    _avatarUrl = avatarUrl;
    [_passengerAvatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl2, avatarUrl]] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
}

@end


/**
 *  handle
 */
@implementation OrderHandleView
{
    UIButton * _wordsButton;
    UIButton * _mapButton;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.backgroundColor = [UIColor whiteColor];
        _wordsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.addSubview(_wordsButton).addSubview(_mapButton);
        [_wordsButton setTitle:@"文字描述" forState:UIControlStateNormal];
        [_mapButton setTitle:@"查看地图" forState:UIControlStateNormal];
        [_wordsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_mapButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_wordsButton setTitleColor:[UIColor colorWithString:@"#F09636"] forState:UIControlStateSelected];
        [_mapButton setTitleColor:[UIColor colorWithString:@"#F09636"] forState:UIControlStateSelected];
        __weak typeof(self) weakSelf = self;
        [_wordsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.equalTo(weakSelf);
            make.width.mas_equalTo(_mapButton.mas_width);
        }];
        [_mapButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.right.equalTo(weakSelf);
            make.left.equalTo(_wordsButton.mas_right);
        }];
        [_wordsButton addTarget:self action:@selector(wordsEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_mapButton addTarget:self action:@selector(mapEvent:) forControlEvents:UIControlEventTouchUpInside];
        _wordsButton.selected = YES;
    }
    return self;
}
- (void)wordsEvent:(UIButton *)sender {
    if([_delegate respondsToSelector:@selector(clickWords:)]) {
        sender.selected = !sender.selected;
        _mapButton.selected = !sender.selected;
        [_delegate clickWords:self];
    }
}

- (void)mapEvent:(UIButton *)sender {
    if([_delegate respondsToSelector:@selector(clickMap:)]) {
        sender.selected = !sender.selected;
        _wordsButton.selected = !sender.selected;
        [_delegate clickMap:self];
    }
}

@end
